package com.wyz.socket.core.base.frame.send;

import com.wyz.socket.core.base.IoArgs;
import com.wyz.socket.core.base.frame.Frame;
import com.wyz.socket.core.base.packet.AbstractSendPacket;

import java.io.IOException;

public abstract class AbstractSendPacketFrame extends AbstractSendFrame {
    protected volatile AbstractSendPacket<?> sendPacket;

    public AbstractSendPacketFrame(int length, byte type, byte flag, short identifier, AbstractSendPacket<?> sendPacket) {
        super(length, type, flag, identifier);
        this.sendPacket = sendPacket;
    }

    public boolean abort(){
        boolean sending = isSending();
        if(sending){
            fillEmptyDataOnFrame() ;
        }
        sendPacket = null ;
        return !sending ;
    }
    protected  void fillEmptyDataOnFrame() {

    }

    public synchronized AbstractSendPacket<?> getpacket(){
        return sendPacket ;
    }
    @Override
    public synchronized boolean handle(IoArgs args) throws IOException {
        if(sendPacket == null && !isSending()){
            return true ; 
        }
        return super.handle(args);
    }

    @Override
    public final synchronized Frame nextFrame() {
        return sendPacket == null ? null : buildNextFrame();
    }

    protected abstract Frame buildNextFrame();

}
